<?php

/*****
* File :    .php
* module :  
* Created:  
* Update:   
* Function: 
* Author:   Eugene Heriniaina
* Email:    hery@serasera.org
* Comment:  
*******/

//manage status


switch($task) {
	case 'new':
		necro_edit( $option );
		break;
	case 'edit':
		if($cid[0]) {
			$id = $cid[0];
		}
		necro_edit( $option, $id );
		break;
	case 'save':
		necro_save( $option, $id );
		break;
	case 'remove';
	case 'delete';
		necro_delete( $option, intval( $cid[0] ) );
		break;
	default:
		if($id) {
			necro_edit( $option, $id );
		} else {
			necro_list( $option );
		}
		break;
}

function necro_delete(  $option, $id ) {
	global $database, $access;

	$row = new congregationNecro( $database );
	$row->load( $id );

	if(!$row->id) {
		echo "<script> alert('". _Necro_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}

	$query = "DELETE FROM #__congregation_status "
	. "\n WHERE id = '$id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( "?option=$option&rub=necro&Itemid=$Itemid");
	
}


function necro_view( $option, $id ) {
	global $my, $access, $database;
	if(!$access->canEdit || !$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationNecro( $database );
	$row->load( $id );


	HTML_necro::necro_view( $row, $option );
}

/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function necro_order( $uid, $inc, $option ) {
	global $database, $option, $rub, $Itemid;

	$row = new congregationNecro( $database );
	$row->load( $uid );
	$row->move( $inc );

	mosRedirect( ("?option=$option&rub=$rub&Itemid=$Itemid") );
}

function necro_list( $option ) {
	global $database;

//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );

	$txtsearch		= trim( mosGetParam( $_REQUEST, 'txtsearch'		,	""		) );

	$where = array();

    if($txtsearch) {
		$tmp = " (p.lname LIKE '%". ereg_replace(" ", "%' OR p.lname LIKE '%", $txtsearch). "%' ";
		$tmp .= " OR p.fname LIKE '%". ereg_replace(" ", "%' OR p.fname LIKE '%", $txtsearch). "%' ";
		$tmp .= " OR u.username LIKE '%". ereg_replace(" ", "%' OR u.username LIKE '%", $txtsearch). "%') ";
    	$where[] = $tmp;
   }
	$query = "SELECT count(n.id) FROM #__congregation_necro AS n  "
	. "\nLEFT JOIN #__users AS u ON n.username=u.username "
	. "\nLEFT JOIN #__congregation_profile AS p ON n.username=p.username "
	. (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	;

	$database->setQuery( $query );
	$total = $database->loadResult();
	
	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );


	$query = "SELECT n.*, p.* FROM #__congregation_necro AS n "
	. "\nLEFT JOIN #__users AS u ON n.username=u.username "
	. "\nLEFT JOIN #__congregation_profile AS p ON n.username=p.username "
	. (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	. "\nORDER BY u.username "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
	
	$database->setQuery( $query );
	$rows =  $database->loadObjectList();

	HTML_necro::necro_list( $rows, $option, $pageNav );

}

function necro_edit( $option, $id = null) {
	global $access, $database;

	$row = new congregationNecro( $database );
	$row->load( $id );

	$lists = array();

	//if new then show the list of all users
	$database->setQuery( "SELECT u.username "
		. "\nFROM #__users AS u "
		. (!$row->username ? "\nLEFT JOIN #__congregation_necro AS n ON u.username=n.username "
		. "\nWHERE n.username IS NULL " : "")
		. "\nORDER BY u.username "
	);
	$lists['users'] = $database->loadObjectList();

  HTML_necro::necro_edit( $row, $option, $lists );
}

function necro_save( $option, $id ) {
	global $Itemid, $rub, $database;

	$row = new congregationNecro( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	
	mosRedirect( ("?option=$option&rub=$rub&Itemid=$Itemid"), "Saved" );


}


class HTML_necro {

	function necro_list( &$rows, $option , &$pageNav ) {
		global $params, $Itemid, $access;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Necro_List; ?>
			</th>
		</tr>
		</table>
		<form action="index2.php" method="get">
		<table border="0" width="100%">
			<tr valign="top">
				<td align="right">
				<?php echo _Profile_Search ?>
				<input type='text' name='txtsearch' size=30 />
				<input type='submit' name='bouton' value="   <?php echo _Profile_Search_Button ;?>   " />
				<input type="hidden" name="option" value="<?php echo $option ;?>" />
				<input type="hidden" name="rub" value="necro" />
				<input type="hidden" name="task" value="list" /><br>
		</table>
		</form>

		<form action="index2.php" method="post" name="adminForm">
		<table class="adminlist">
				<tr valign="top">
					<th width="20">
					#
					</th>
					<th width="20">
					<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $rows ); ?>);" />
					</th>
					<th><?php echo _Profile_Username ; ?> </th>
					<th><?php echo _Necro_Author ; ?></th>
				</tr>
				<?php
				if($rows) {
				$k = 0;
				for ($i=0, $n=count( $rows ); $i < $n; $i++) {
					$row = &$rows[$i];
					$k = 1 - $k;

					$link 		= 'index2.php?option=com_congregation&rub=necro&task=edit&hidemainmenu=1&id='. $row->id;


					$checked 	= mosCommonHTML::CheckedOutProcessing( $row, $i );
					?>

			<tr class="<?php echo "row$k"; ?>">
				<td  width="20">
				<?php echo $pageNav->rowNumber( $i ); ?>
				</td>
				<td  width="20">
				<?php echo $checked; ?>
				</td>
				<td><?php echo ($row->lname || $row->fname) ? $row->lname . " " . $row->fname : $row->username ; ?> </td>
				<td><?php echo $row->author;  ?></td>
				</tr>
						<?php

					}
				}
				?>
			</table>
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="rub" value="necro" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		<?php
		echo $pageNav->getListFooter(); 
		?>
		</form>
<?php
	
	}

	function necro_edit( &$row, $option, &$lists ) {
		global $params, $Itemid;
			?>
		<table class="adminheading">
		<tr>
			<th>
			<?php echo _Necro_Edit; ?>
			</th>
		</tr>
		</table>
		<script language="javascript" type="text/javascript">
		<!--

		function submitbutton(pressbutton) {
			var form = document.adminForm;

			if (pressbutton == 'cancel') {
				submitform( pressbutton );
				return;
			}

			// do field validation
			if (form.username.value == ""){
				alert( "You didn't enter the username" );
			} else {
				<?php getEditorContents( 'editor1', 'texte' ) ; ?>
				submitform( pressbutton );
			}
		}
		//-->
		</script>
		<form action="index2.php" method="post" name="adminForm">
					<input type="hidden" name="id" value="<?php echo $row->id ;?>" />
					<input type="hidden" name="option" value="<?php echo $option ;?>" />
					<input type="hidden" name="Itemid" value="<?php echo $Itemid ;?>" />
					<input type="hidden" name="rub" value="necro" />
					<input type="hidden" name="task" value="save" />
		<table class="adminform">
				<tr valign="top">
					<td><?php echo _Profile_Username ; ?> : </td>
					<td><select name='username'><option value="">Select here</option>
	  <?php
	  foreach($lists['users'] as $users) {
	  	$selected = ($users->username == $row->username) ? " selected " : "";
		echo  "<option value='" . $users->username . "' $selected >" . $users->username . "</option>";
	  }
	  ?></select></td>
				</tr>
				<tr>
					<td><?php echo _Necro_Texte ; ?> : </td>
					<td valign='top'><?php
					// parameters : areaname, content, hidden field, width, height, rows, cols
					editorArea( 'editor1', $row->texte, 'texte', '100%;', '400', '75', '30' ) ; ?>
					</td>
				</tr>
				<tr>
					<td valign="top"><?php echo _Necro_Author ; ?> : </td>
					<td><input type="text" name="author" class="inputbox" value="<?php echo $row->author ; ?>"size="30" maxlength="255" /></td>
				</tr>
			</table>
			</form>
	<?php

	}

}

class congregationNecro extends mosDBTable {
	var $id = null;
	var $username = null;
	var $author = null;
	var $date = null;
	var $texte = null;

	function congregationNecro( &$db ) {
		$this->mosDBTable( '#__congregation_necro', 'id', $db );
	}
}

?>